
#ifndef _DYNAMIC_H_
#define _DYNAMIC_H_

#include "dlload.h"


typedef int (*fpgetValue)(char* name, char* type, void* value);
typedef int (*fpsetValue)(char* name, char* type, void* value);
typedef int (*fpStMain)();

fpgetValue getValue = NULL;
fpsetValue setValue = NULL;
fpStMain stMain = NULL;
extern HMODULE mHandle;


class Dynamic
{
public:
    /* 加载动态库 */
    static int initLadder()
    {
        if (mHandle != NULL) {
            if (FreeLibrary(mHandle)) {
                mHandle = NULL;
            }
        }
        mHandle = gdl_Open("variable.dll");
        if (mHandle) {
            getValue = (fpgetValue)gdl_GetProc(mHandle, "getValue");
            setValue = (fpsetValue)gdl_GetProc(mHandle, "setValue");
            stMain = (fpStMain)gdl_GetProc(mHandle, "main");
            return 1;

        }
        return 0;
    }
    /*调用动态库*/
    static int ladderScan()
    {
        if (mHandle) {
            getValue = (fpgetValue)gdl_GetProc(mHandle, "getValue");
            setValue = (fpsetValue)gdl_GetProc(mHandle, "setValue");
            stMain = (fpStMain)gdl_GetProc(mHandle, "main");
            return 1;

        }
    }

    /* 编译动态库 */
    static bool buildLibrary()
    {
        system("rustyc.exe -c test.st -o test.o");
        system("gcc -g -shared test.o _variable.c _getValue.c _setValue.c stlib.c -o variable.dll");
        return 1;
    }

    /* 卸载动态库 */
    static int uninstall()
    {
        if (mHandle != NULL) {

            if (gdl_Close(mHandle)) {
                mHandle = NULL;
            }
        }
        return 1;
    }

};

#endif // !_DYNAMIC_H_

